package com.example.activititest.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.activititest.bean.DeploymentDTO;
import com.example.activititest.service.ActivitiDeploymentService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("activitiDeployment")
public class ActivitiDeploymentServiceImpl implements ActivitiDeploymentService {
	@Autowired
	private RepositoryService repositoryService;

	@Override
	public List<DeploymentDTO> queryPage(Map<String, Object> params) {
		int page = 1;
		int pageSize = 15;
		if (params.get("page") != null) {
			page = Integer.parseInt((String) params.get("page"));
		}
		if (params.get("pageSize") != null) {
			pageSize = Integer.parseInt((String) params.get("pageSize"));
		}
		DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery().orderByDeploymenTime().desc();
		if (params.get("key") != null) {
			deploymentQuery.deploymentKey(params.get("key") + "").latest();
		}
		if (params.get("category") != null) {
			deploymentQuery.deploymentCategory(params.get("category") + "");
		}
		List<Deployment> entitys = deploymentQuery.listPage((page - 1) * pageSize, page * pageSize);
		List<DeploymentDTO> dtos = new ArrayList<DeploymentDTO>();
		if (entitys != null && !entitys.isEmpty()) {
			for (Deployment entity : entitys) {
				dtos.add(new DeploymentDTO(entity));
			}
		}
		return dtos;
	}
}
